package com.easyjop.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.easyjop.entity.constants.Constants;
import com.easyjop.entity.enums.ResponseCodeEnum;
import com.easyjop.exception.BusinessException;
import org.springframework.stereotype.Service;

import com.easyjop.entity.enums.PageSize;
import com.easyjop.entity.query.ShareInfoQuery;
import com.easyjop.entity.po.ShareInfo;
import com.easyjop.entity.vo.PaginationResultVO;
import com.easyjop.entity.query.SimplePage;
import com.easyjop.mappers.ShareInfoMapper;
import com.easyjop.service.ShareInfoService;
import com.easyjop.utils.StringTools;
import org.springframework.transaction.annotation.Transactional;


/**
 * 文章 业务接口实现
 */
@Service("shareInfoService")
public class ShareInfoServiceImpl implements ShareInfoService {

	@Resource
	private ShareInfoMapper<ShareInfo, ShareInfoQuery> shareInfoMapper;

	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<ShareInfo> findListByParam(ShareInfoQuery param) {
		return this.shareInfoMapper.selectList(param);
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public Integer findCountByParam(ShareInfoQuery param) {
		return this.shareInfoMapper.selectCount(param);
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public PaginationResultVO<ShareInfo> findListByPage(ShareInfoQuery param) {
		int count = this.findCountByParam(param);
		int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

		SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
		param.setSimplePage(page);
		List<ShareInfo> list = this.findListByParam(param);
		PaginationResultVO<ShareInfo> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(ShareInfo bean) {
		return this.shareInfoMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<ShareInfo> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.shareInfoMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<ShareInfo> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.shareInfoMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Integer updateByParam(ShareInfo bean, ShareInfoQuery param) {
		StringTools.checkParam(param);
		return this.shareInfoMapper.updateByParam(bean, param);
	}

	/**
	 * 多条件删除
	 */
	@Override
	public Integer deleteByParam(ShareInfoQuery param) {
		StringTools.checkParam(param);
		return this.shareInfoMapper.deleteByParam(param);
	}

	/**
	 * 根据ShareId获取对象
	 */
	@Override
	public ShareInfo getShareInfoByShareId(Integer shareId) {
		return this.shareInfoMapper.selectByShareId(shareId);
	}

	/**
	 * 根据ShareId修改
	 */
	@Override
	public Integer updateShareInfoByShareId(ShareInfo bean, Integer shareId) {
		return this.shareInfoMapper.updateByShareId(bean, shareId);
	}

	/**
	 * 根据ShareId删除
	 */
	@Override
	public Integer deleteShareInfoByShareId(Integer shareId) {
		return this.shareInfoMapper.deleteByShareId(shareId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveShare(ShareInfo bean, Boolean superAdmin) {
		if (bean.getShareId()==null){
			/*新增*/
			bean.setCreateTime(new Date());
			this.shareInfoMapper.insert(bean);
		}
		else {
			ShareInfo dbInfo = this.shareInfoMapper.selectByShareId(bean.getShareId());
			/*//只能自己发布的才能修改*/
			if (!dbInfo.getCreateUserId().equals(bean.getCreateUserId()) && !superAdmin) {
				throw new BusinessException(ResponseCodeEnum.CODE_600);
			}
			bean.setCreateUserId(null);
			bean.setCreateUserName(null);
			bean.setCreateTime(null);
			this.shareInfoMapper.updateByShareId(bean, bean.getShareId());
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delShareBatch(String shareIds, Integer integer) {
		String[] shares=shareIds.split(",");
		if (integer!=null){
			ShareInfoQuery infoQuery = new ShareInfoQuery();
			infoQuery.setShareIds(shares);
			List<ShareInfo> shareInfos = this.shareInfoMapper.selectList(infoQuery);
			List<ShareInfo> collect = shareInfos.stream().filter(itm -> !itm.getCreateUserId().equals(integer)).collect(Collectors.toList());
			if (collect.size()>0){
				throw new BusinessException(ResponseCodeEnum.CODE_606);
			}
		}
		ShareInfoQuery infoQuery = new ShareInfoQuery();
		infoQuery.setShareIds(shares);
		this.shareInfoMapper.deleteByParam(infoQuery);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateStatus(String shareIds, Integer status) {
		ShareInfoQuery infoQuery = new ShareInfoQuery();
		infoQuery.setShareIds(shareIds.split(","));
		ShareInfo info = new ShareInfo();
		info.setStatus(status);
		updateByParam(info,infoQuery);
	}
	@Override
	public ShareInfo showShareDetailNext(ShareInfoQuery query, Integer type, Integer currentId, boolean updateReadCount) {
		if (type == null) {
			query.setShareId(currentId);
		} else {
			query.setNextType(type);
			query.setCurrentId(currentId);
		}
		ShareInfo shareInfo = shareInfoMapper.showDetailNext(query);
		if (shareInfo == null && type == -1) {
			throw new BusinessException("已经是第一条了");
		} else if (shareInfo == null && type == 1) {
			throw new BusinessException("已经是最后一条了");
		}
		if (updateReadCount && shareInfo != null) {
			//更新阅读数
			this.shareInfoMapper.updateCount(1,shareInfo.getShareId());
			shareInfo.setReadCount(shareInfo.getReadCount() + 1);
		}
		return shareInfo;
	}
}